﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using SnippetDoc.Entities;
using SnippetDoc.Utils;

namespace SnippetDoc.UnitTests.Utils
{
    /// <summary>
    /// Summary description for CodeSampleInterpreterTest
    /// </summary>
    [TestClass]
    public class CodeSampleInterpreterTest
    {
        [TestMethod]
        public void TestInterpretSnippetTemplate_ValidSnippetDetail_ShouldResolveCorrectly()
        {
            //Arrange
            SnippetDetail detail = new SnippetDetail
            {
                VariableDeclarations = new List<VariableDeclaration>()
                {
                    new VariableDeclaration{
                         Id = "varName",
                          DefaultValue = "aCar"
                    },
                    new VariableDeclaration{
                           Id = "varType",
                           DefaultValue = "Car"
                    }
                },
                 SnippetTemplate = @"var $varName$ = new $varType$();"
            };

            string expectedSample = "var aCar = new Car();";

            //Act
            string interpretedSample = new CodeSampleInterpreter().InterpreteSnippetTemplate(detail);

            //Assert
            Assert.AreEqual(expectedSample, interpretedSample, "The code samples should match");
        }

        [TestMethod]
        public void TestInterpretSnippetTemplate_NullSnippet_ShouldReturnNull()
        {
            //Arrange


            //Act
            string interpretedSample = new CodeSampleInterpreter().InterpreteSnippetTemplate(null);

            //Assert
            Assert.IsNull(interpretedSample, "The intepreted sample should be null");
        }

        [TestMethod]
        public void TestInterpretSnippetTemplate_NoVariableDeclarations_ShouldResolveSnippetWithoutVariables()
        {
            //Arrange
            SnippetDetail detail = new SnippetDetail
            {
                SnippetTemplate = @"var test = new Test();"
            };

            string expectedSample = "var test = new Test();";

            //Act
            string interpretedSample = new CodeSampleInterpreter().InterpreteSnippetTemplate(detail);

            //Assert
            Assert.AreEqual(expectedSample, interpretedSample, "The code samples should match");
        }

        [TestMethod]
        public void TestInterpretSnippetTemplate_EndAnchorInTemplate_AnchorShouldBeRemoved()
        {
            //Arrange
            SnippetDetail detail = new SnippetDetail
            {
                VariableDeclarations = new List<VariableDeclaration>()
                {
                    new VariableDeclaration{
                         Id = "varName",
                          DefaultValue = "aCar"
                    },
                    new VariableDeclaration{
                           Id = "varType",
                           DefaultValue = "Car"
                    }
                },
                SnippetTemplate = @"var $varName$ = new $varType$();$end$"
            };

            string expectedSample = "var aCar = new Car();";

            //Act
            string interpretedSample = new CodeSampleInterpreter().InterpreteSnippetTemplate(detail);

            //Assert
            Assert.AreEqual(expectedSample, interpretedSample, "The code samples should match");
        }
    }
}
